package com.zf.shuashua.advert.mg;

import android.os.Handler
import android.os.Looper
import androidx.annotation.NonNull

abstract class BaseAdProvider : IAdProvider {

    /**
     * --------------------------- 激励广告 ---------------------------
     */
    fun callbackRewardStartRequest(@NonNull adProviderType: String, @NonNull listener: RewardListener) {
        Handler(Looper.getMainLooper()).post {
            "${adProviderType}: 开始请求".logi()
            listener.onAdStartRequest(adProviderType)
        }
    }

    fun callbackRewardLoaded(@NonNull adProviderType: String, @NonNull listener: RewardListener) {
        Handler(Looper.getMainLooper()).post {
            "${adProviderType}: 请求成功了".logi()
            listener.onAdLoaded(adProviderType)
        }
    }

    fun callbackRewardFailed(@NonNull adProviderType: String, @NonNull listener: RewardListener, failedMsg: String?) {
        Handler(Looper.getMainLooper()).post {
            "${adProviderType}: 请求失败了：$failedMsg".loge()
            listener.onAdFailed(adProviderType, failedMsg)
        }
    }

    fun callbackRewardClicked(@NonNull adProviderType: String, @NonNull listener: RewardListener) {
        Handler(Looper.getMainLooper()).post {
            "${adProviderType}: 点击了".logi()
//            sdkReport(adProviderType, ReportLocationType.Click.type)
            listener.onAdClicked(adProviderType)
        }
    }

    fun callbackRewardShow(@NonNull adProviderType: String, @NonNull listener: RewardListener) {
        Handler(Looper.getMainLooper()).post {
            "${adProviderType}: 展示了".logi()
            listener.onAdShow(adProviderType)
        }
    }

    fun callbackRewardExpose(@NonNull adProviderType: String, @NonNull listener: RewardListener) {
        Handler(Looper.getMainLooper()).post {
            "${adProviderType}: 曝光了".logi()
//            sdkReport(adProviderType, ReportLocationType.Show.type)
            listener.onAdExpose(adProviderType)
        }
    }

    fun callbackRewardVideoComplete(@NonNull adProviderType: String, @NonNull listener: RewardListener) {
        Handler(Looper.getMainLooper()).post {
            "${adProviderType}: 播放完成".logi()
//            sdkReport(adProviderType, ReportLocationType.startInstall.type)
            listener.onAdVideoComplete(adProviderType)
        }
    }

    fun callbackRewardVideoCached(@NonNull adProviderType: String, @NonNull listener: RewardListener) {
        Handler(Looper.getMainLooper()).post {
            "${adProviderType}: 视频已缓存".logi()
            listener.onAdVideoCached(adProviderType)
        }
    }

    fun callbackRewardVerify(@NonNull adProviderType: String, @NonNull listener: RewardListener) {
        Handler(Looper.getMainLooper()).post {
            "${adProviderType}: 激励验证".logi()
            listener.onAdRewardVerify(adProviderType)
        }
    }

    fun callbackRewardClose(@NonNull adProviderType: String, @NonNull listener: RewardListener) {
        Handler(Looper.getMainLooper()).post {
            "${adProviderType}: 关闭了".logi()
            listener.onAdClose(adProviderType)
        }
    }

    //TODO:
//    private fun sdkReport(adProviderType: String, reportType: Int) {
//        when (adProviderType) {
//            AdProviderType.BAIDU.type -> {
//                startSdkReport(reportType, SDKType.BAIDU.type)
//            }
//            AdProviderType.GDT.type -> {
//                startSdkReport(reportType, SDKType.GDT.type)
//            }
//            AdProviderType.CSJ.type -> {
//                startSdkReport(reportType, SDKType.CSJ.type)
//            }
//        }
//    }
//
//    private fun startSdkReport(reportType: Int, sdkType: Int) {
//        val sdkConfig = SpKit.getSDKConfig() ?: return
//        sdkConfig.sdks.forEach { sdkBean ->
//            if (sdkType == sdkBean.type) {
//                SdkApi.sdkReport(reportType, sdkBean.sdkId, object : SdkApi.CommonCallback<String> {
//                    override fun onSuccess(t: String?) {
//                        "sdk汇报状态：$$t".logi()
//                    }
//
//                    override fun onError(errMsg: String?) {
//                        "SDK汇报出错：$$errMsg".loge()
//                    }
//                })
//            }
//        }
//    }
}